package com.ihr360.authority.sharing.dao;

import com.ihr360.authority.sharing.model.dto.AuthUserResource;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface AuthUserResourceRepository extends CrudRepository<AuthUserResource, Long> {

    /**
     * 获取某个人的某个模块下可操作的资源列表
     * @param companyId
     * @param bizModel 模块标识
     * @param userId
     * @return
     */
    @Query(value = " select j  from AuthUserResource j " +
            " where  companyId = :companyId and bizModel=:bizModel and (userId=:userId or publicity=8) order by privilege asc")
    List<AuthUserResource> queryUserResources(
            @Param(value = "companyId") String companyId,
            @Param(value = "bizModel") String bizModel,
            @Param(value = "userId") String userId);


    /**
     * 获取某个人在某个模块下的可操作资源列表
     * @param companyId
     * @param bizModel
     * @param userId
     * @param pageable
     * @return
     */


    @Query(value = " select j from AuthUserResource j " +
            " where  companyId = :companyId and bizModel=:bizModel and (userId=:userId or publicity =8)")
    Page<AuthUserResource> findAccessableBizResources(
            @Param(value = "companyId") String companyId,
            @Param(value = "bizModel") String bizModel,
            @Param(value = "userId") String userId, Pageable pageable);

    /**
     * 获取某个资源的可操作权限
     * @param companyId
     * @param bizModel
     * @param userId
     * @param resourceId
     * @return
     */
    @Query(value = "select j  from AuthUserResource j " +
            " where  companyId = :companyId and bizModel=:bizModel and resourceId=:resourceId and (userId=:userId or publicity=8) order by privilege desc")
    List<AuthUserResource> findResourcePrivilege(
            @Param(value = "companyId") String companyId,
            @Param(value = "bizModel") String bizModel,
            @Param(value = "userId") String userId,
            @Param(value = "resourceId") String resourceId);

    /**
     * 获取某个资源的可操作人员列表
     * @param companyId
     * @param bizModel
     * @param resourceId
     * @param pageable
     * @return
     */
    Page<AuthUserResource> findByCompanyIdAndBizModelAndResourceId(
            @Param(value = "companyId") String companyId,
            @Param(value = "bizModel") String bizModel,
            @Param(value = "resourceId") String resourceId, Pageable pageable);

    List<AuthUserResource> findAllByCompanyIdAndAndBizModelAndResourceId(
            @Param(value = "companyId") String companyId,
            @Param(value = "bizModel") String bizModel,
            @Param(value = "resourceId") String resourceId);


    @Query(value = " select count(1) from AuthUserResource j " +
            " where  companyId = :companyId and bizModel=:bizModel and userId=:userId and resourceId=:resourceId", nativeQuery = true)
    int queryUserResourceCount(
            @Param(value = "companyId") String companyId,
            @Param(value = "bizModel") String bizModel,
            @Param(value = "userId") String userId,
            @Param(value = "resourceId") String resourceId);

    @Modifying
    void deleteByCompanyIdAndBizModelAndResourceId(
            @Param(value = "companyId") String companyId,
            @Param(value = "bizModel") String bizModel,
            @Param(value = "resourceId") String resourceId);

    /**
     * getOne
     *
     * @param companyId
     * @param bizModel
     * @param userId
     * @param resourceId
     * @return
     */
    @Query(value = "select j  from AuthUserResource j " +
            " where  companyId = :companyId and bizModel=:bizModel and resourceId=:resourceId and userId=:userId")
    AuthUserResource takeOne(
            @Param(value = "companyId") String companyId,
            @Param(value = "bizModel") String bizModel,
            @Param(value = "userId") String userId,
            @Param(value = "resourceId") String resourceId);

    List<AuthUserResource> findAllByCompanyIdAndBizModelAndResourceIdAndUserIdIn(
            @Param(value = "companyId") String companyId,
            @Param(value = "bizModel") String bizModel,
            @Param(value = "resourceId") String resourceId,
            @Param(value = "userIdList")List<String> userIdList);
}